{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e9190d61",
   "metadata": {},
   "source": [
    "# Using Opik with Smolagents\n",
    "\n",
    "Opik integrates seamlessly with Smolagents, a framework from HuggingFace to create Agents. The integration leverages Opik's built-in OpenTelemetry support."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d8a362ab",
   "metadata": {},
   "source": [
    "## Creating an account on Comet.com\n",
    "\n",
    "[Comet](https://www.comet.com/site?from=llm&utm_source=opik&utm_medium=colab&utm_content=openai&utm_campaign=opik) provides a hosted version of the Opik platform, [simply create an account](https://www.comet.com/signup?from=llm&utm_source=opik&utm_medium=colab&utm_content=openai&utm_campaign=opik) and grab your API Key.\n",
    "\n",
    "> You can also run the Opik platform locally, see the [installation guide](https://www.comet.com/docs/opik/self-host/overview/?from=llm&utm_source=opik&utm_medium=colab&utm_content=openai&utm_campaign=opik) for more information."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "17875df5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: opik in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (1.7.26)\n",
      "Collecting smolagents[telemetry,toolkit]\n",
      "  Downloading smolagents-1.16.1-py3-none-any.whl.metadata (16 kB)\n",
      "Collecting huggingface-hub>=0.31.2 (from smolagents[telemetry,toolkit])\n",
      "  Downloading huggingface_hub-0.31.4-py3-none-any.whl.metadata (13 kB)\n",
      "Requirement already satisfied: requests>=2.32.3 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from smolagents[telemetry,toolkit]) (2.32.3)\n",
      "Requirement already satisfied: rich>=13.9.4 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from smolagents[telemetry,toolkit]) (13.9.4)\n",
      "Requirement already satisfied: jinja2>=3.1.4 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from smolagents[telemetry,toolkit]) (3.1.4)\n",
      "Requirement already satisfied: pillow>=10.0.1 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from smolagents[telemetry,toolkit]) (11.2.1)\n",
      "Requirement already satisfied: python-dotenv in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from smolagents[telemetry,toolkit]) (1.0.1)\n",
      "Requirement already satisfied: arize-phoenix in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from smolagents[telemetry,toolkit]) (8.14.0)\n",
      "Requirement already satisfied: opentelemetry-sdk in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from smolagents[telemetry,toolkit]) (1.32.1)\n",
      "Requirement already satisfied: opentelemetry-exporter-otlp in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from smolagents[telemetry,toolkit]) (1.32.1)\n",
      "Collecting openinference-instrumentation-smolagents>=0.1.4 (from smolagents[telemetry,toolkit])\n",
      "  Downloading openinference_instrumentation_smolagents-0.1.11-py3-none-any.whl.metadata (4.5 kB)\n",
      "Requirement already satisfied: duckduckgo-search>=6.3.7 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from smolagents[telemetry,toolkit]) (6.3.7)\n",
      "Requirement already satisfied: markdownify>=0.14.1 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from smolagents[telemetry,toolkit]) (0.14.1)\n",
      "Requirement already satisfied: boto3-stubs>=1.34.110 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from boto3-stubs[bedrock-runtime]>=1.34.110->opik) (1.36.20)\n",
      "Requirement already satisfied: click in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from opik) (8.1.8)\n",
      "Requirement already satisfied: httpx in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from opik) (0.28.1)\n",
      "Requirement already satisfied: levenshtein<1.0.0 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from opik) (0.26.1)\n",
      "Requirement already satisfied: litellm in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from opik) (1.67.2)\n",
      "Requirement already satisfied: openai<2.0.0 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from opik) (1.76.2)\n",
      "Requirement already satisfied: pydantic-settings!=2.9.0,<3.0.0,>=2.0.0 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from opik) (2.7.0)\n",
      "Requirement already satisfied: pydantic<3.0.0,>=2.0.0 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from opik) (2.11.4)\n",
      "Requirement already satisfied: pytest in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from opik) (8.3.5)\n",
      "Requirement already satisfied: sentry_sdk>=2.0.0 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from opik) (2.14.0)\n",
      "Requirement already satisfied: tenacity in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from opik) (9.1.2)\n",
      "Requirement already satisfied: tqdm in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from opik) (4.67.1)\n",
      "Requirement already satisfied: uuid6 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from opik) (2024.7.10)\n",
      "Requirement already satisfied: botocore-stubs in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from boto3-stubs>=1.34.110->boto3-stubs[bedrock-runtime]>=1.34.110->opik) (1.36.20)\n",
      "Requirement already satisfied: types-s3transfer in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from boto3-stubs>=1.34.110->boto3-stubs[bedrock-runtime]>=1.34.110->opik) (0.11.2)\n",
      "Requirement already satisfied: mypy-boto3-bedrock-runtime<1.37.0,>=1.36.0 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from boto3-stubs[bedrock-runtime]>=1.34.110->opik) (1.36.2)\n",
      "Requirement already satisfied: primp>=0.8.1 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from duckduckgo-search>=6.3.7->smolagents[telemetry,toolkit]) (0.10.0)\n",
      "Requirement already satisfied: filelock in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from huggingface-hub>=0.31.2->smolagents[telemetry,toolkit]) (3.16.1)\n",
      "Requirement already satisfied: fsspec>=2023.5.0 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from huggingface-hub>=0.31.2->smolagents[telemetry,toolkit]) (2024.6.1)\n",
      "Requirement already satisfied: packaging>=20.9 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from huggingface-hub>=0.31.2->smolagents[telemetry,toolkit]) (23.2)\n",
      "Requirement already satisfied: pyyaml>=5.1 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from huggingface-hub>=0.31.2->smolagents[telemetry,toolkit]) (6.0.2)\n",
      "Requirement already satisfied: typing-extensions>=3.7.4.3 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from huggingface-hub>=0.31.2->smolagents[telemetry,toolkit]) (4.13.2)\n",
      "Requirement already satisfied: MarkupSafe>=2.0 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from jinja2>=3.1.4->smolagents[telemetry,toolkit]) (3.0.2)\n",
      "Requirement already satisfied: rapidfuzz<4.0.0,>=3.9.0 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from levenshtein<1.0.0->opik) (3.11.0)\n",
      "Requirement already satisfied: beautifulsoup4<5,>=4.9 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from markdownify>=0.14.1->smolagents[telemetry,toolkit]) (4.12.3)\n",
      "Requirement already satisfied: six<2,>=1.15 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from markdownify>=0.14.1->smolagents[telemetry,toolkit]) (1.16.0)\n",
      "Requirement already satisfied: anyio<5,>=3.5.0 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from openai<2.0.0->opik) (4.9.0)\n",
      "Requirement already satisfied: distro<2,>=1.7.0 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from openai<2.0.0->opik) (1.9.0)\n",
      "Requirement already satisfied: jiter<1,>=0.4.0 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from openai<2.0.0->opik) (0.8.2)\n",
      "Requirement already satisfied: sniffio in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from openai<2.0.0->opik) (1.3.1)\n",
      "Requirement already satisfied: certifi in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from httpx->opik) (2025.4.26)\n",
      "Requirement already satisfied: httpcore==1.* in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from httpx->opik) (1.0.9)\n",
      "Requirement already satisfied: idna in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from httpx->opik) (3.10)\n",
      "Requirement already satisfied: h11>=0.16 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from httpcore==1.*->httpx->opik) (0.16.0)\n",
      "Collecting openinference-instrumentation>=0.1.27 (from openinference-instrumentation-smolagents>=0.1.4->smolagents[telemetry,toolkit])\n",
      "  Downloading openinference_instrumentation-0.1.30-py3-none-any.whl.metadata (4.9 kB)\n",
      "Collecting openinference-semantic-conventions>=0.1.17 (from openinference-instrumentation-smolagents>=0.1.4->smolagents[telemetry,toolkit])\n",
      "  Downloading openinference_semantic_conventions-0.1.17-py3-none-any.whl.metadata (1.2 kB)\n",
      "Requirement already satisfied: opentelemetry-api in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from openinference-instrumentation-smolagents>=0.1.4->smolagents[telemetry,toolkit]) (1.32.1)\n",
      "Requirement already satisfied: opentelemetry-instrumentation in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from openinference-instrumentation-smolagents>=0.1.4->smolagents[telemetry,toolkit]) (0.53b1)\n",
      "Requirement already satisfied: opentelemetry-semantic-conventions in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from openinference-instrumentation-smolagents>=0.1.4->smolagents[telemetry,toolkit]) (0.53b1)\n",
      "Requirement already satisfied: wrapt in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from openinference-instrumentation-smolagents>=0.1.4->smolagents[telemetry,toolkit]) (1.17.2)\n",
      "Requirement already satisfied: annotated-types>=0.6.0 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from pydantic<3.0.0,>=2.0.0->opik) (0.7.0)\n",
      "Requirement already satisfied: pydantic-core==2.33.2 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from pydantic<3.0.0,>=2.0.0->opik) (2.33.2)\n",
      "Requirement already satisfied: typing-inspection>=0.4.0 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from pydantic<3.0.0,>=2.0.0->opik) (0.4.0)\n",
      "Requirement already satisfied: charset-normalizer<4,>=2 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from requests>=2.32.3->smolagents[telemetry,toolkit]) (3.4.1)\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from requests>=2.32.3->smolagents[telemetry,toolkit]) (2.0.7)\n",
      "Requirement already satisfied: markdown-it-py>=2.2.0 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from rich>=13.9.4->smolagents[telemetry,toolkit]) (3.0.0)\n",
      "Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from rich>=13.9.4->smolagents[telemetry,toolkit]) (2.18.0)\n",
      "Requirement already satisfied: aioitertools in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from arize-phoenix->smolagents[telemetry,toolkit]) (0.12.0)\n",
      "Requirement already satisfied: aiosqlite in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from arize-phoenix->smolagents[telemetry,toolkit]) (0.20.0)\n",
      "Requirement already satisfied: alembic<2,>=1.3.0 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from arize-phoenix->smolagents[telemetry,toolkit]) (1.14.0)\n",
      "Requirement already satisfied: arize-phoenix-client in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from arize-phoenix->smolagents[telemetry,toolkit]) (1.1.0)\n",
      "Requirement already satisfied: arize-phoenix-evals>=0.13.1 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from arize-phoenix->smolagents[telemetry,toolkit]) (0.17.2)\n",
      "Requirement already satisfied: arize-phoenix-otel>=0.5.1 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from arize-phoenix->smolagents[telemetry,toolkit]) (0.6.1)\n",
      "Requirement already satisfied: authlib in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from arize-phoenix->smolagents[telemetry,toolkit]) (1.5.2)\n",
      "Requirement already satisfied: cachetools in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from arize-phoenix->smolagents[telemetry,toolkit]) (5.5.0)\n",
      "Requirement already satisfied: fastapi in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from arize-phoenix->smolagents[telemetry,toolkit]) (0.115.12)\n",
      "Requirement already satisfied: grpc-interceptor in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from arize-phoenix->smolagents[telemetry,toolkit]) (0.15.4)\n",
      "Requirement already satisfied: grpcio in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from arize-phoenix->smolagents[telemetry,toolkit]) (1.72.0rc1)\n",
      "Requirement already satisfied: numpy!=2.0.0 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from arize-phoenix->smolagents[telemetry,toolkit]) (1.26.4)\n",
      "Requirement already satisfied: opentelemetry-proto>=1.12.0 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from arize-phoenix->smolagents[telemetry,toolkit]) (1.32.1)\n",
      "Requirement already satisfied: pandas>=1.0 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from arize-phoenix->smolagents[telemetry,toolkit]) (2.2.3)\n",
      "Requirement already satisfied: protobuf<6.0,>=3.20.2 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from arize-phoenix->smolagents[telemetry,toolkit]) (5.29.3)\n",
      "Requirement already satisfied: psutil in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from arize-phoenix->smolagents[telemetry,toolkit]) (5.9.0)\n",
      "Requirement already satisfied: pyarrow in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from arize-phoenix->smolagents[telemetry,toolkit]) (18.1.0)\n",
      "Requirement already satisfied: python-multipart in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from arize-phoenix->smolagents[telemetry,toolkit]) (0.0.9)\n",
      "Requirement already satisfied: scikit-learn in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from arize-phoenix->smolagents[telemetry,toolkit]) (1.5.1)\n",
      "Requirement already satisfied: scipy in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from arize-phoenix->smolagents[telemetry,toolkit]) (1.14.1)\n",
      "Requirement already satisfied: sqlalchemy<3,>=2.0.4 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from sqlalchemy[asyncio]<3,>=2.0.4->arize-phoenix->smolagents[telemetry,toolkit]) (2.0.32)\n",
      "Requirement already satisfied: sqlean-py>=3.45.1 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from arize-phoenix->smolagents[telemetry,toolkit]) (3.47.0)\n",
      "Requirement already satisfied: starlette in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from arize-phoenix->smolagents[telemetry,toolkit]) (0.46.2)\n",
      "Requirement already satisfied: strawberry-graphql>=0.262.0 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from arize-phoenix->smolagents[telemetry,toolkit]) (0.262.5)\n",
      "Requirement already satisfied: uvicorn in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from arize-phoenix->smolagents[telemetry,toolkit]) (0.34.2)\n",
      "Requirement already satisfied: websockets in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from arize-phoenix->smolagents[telemetry,toolkit]) (15.0.1)\n",
      "Requirement already satisfied: aiohttp in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from litellm->opik) (3.11.8)\n",
      "Requirement already satisfied: importlib-metadata>=6.8.0 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from litellm->opik) (8.4.0)\n",
      "Requirement already satisfied: jsonschema<5.0.0,>=4.22.0 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from litellm->opik) (4.23.0)\n",
      "Requirement already satisfied: tiktoken>=0.7.0 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from litellm->opik) (0.8.0)\n",
      "Requirement already satisfied: tokenizers in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from litellm->opik) (0.19.1)\n",
      "Requirement already satisfied: opentelemetry-exporter-otlp-proto-grpc==1.32.1 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from opentelemetry-exporter-otlp->smolagents[telemetry,toolkit]) (1.32.1)\n",
      "Requirement already satisfied: opentelemetry-exporter-otlp-proto-http==1.32.1 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from opentelemetry-exporter-otlp->smolagents[telemetry,toolkit]) (1.32.1)\n",
      "Requirement already satisfied: deprecated>=1.2.6 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from opentelemetry-exporter-otlp-proto-grpc==1.32.1->opentelemetry-exporter-otlp->smolagents[telemetry,toolkit]) (1.2.14)\n",
      "Requirement already satisfied: googleapis-common-protos~=1.52 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from opentelemetry-exporter-otlp-proto-grpc==1.32.1->opentelemetry-exporter-otlp->smolagents[telemetry,toolkit]) (1.63.2)\n",
      "Requirement already satisfied: opentelemetry-exporter-otlp-proto-common==1.32.1 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from opentelemetry-exporter-otlp-proto-grpc==1.32.1->opentelemetry-exporter-otlp->smolagents[telemetry,toolkit]) (1.32.1)\n",
      "Requirement already satisfied: iniconfig in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from pytest->opik) (2.0.0)\n",
      "Requirement already satisfied: pluggy<2,>=1.5 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from pytest->opik) (1.5.0)\n",
      "Requirement already satisfied: Mako in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from alembic<2,>=1.3.0->arize-phoenix->smolagents[telemetry,toolkit]) (1.3.6)\n",
      "Requirement already satisfied: soupsieve>1.2 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from beautifulsoup4<5,>=4.9->markdownify>=0.14.1->smolagents[telemetry,toolkit]) (2.5)\n",
      "Requirement already satisfied: zipp>=0.5 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from importlib-metadata>=6.8.0->litellm->opik) (3.21.0)\n",
      "Requirement already satisfied: attrs>=22.2.0 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from jsonschema<5.0.0,>=4.22.0->litellm->opik) (24.3.0)\n",
      "Requirement already satisfied: jsonschema-specifications>=2023.03.6 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from jsonschema<5.0.0,>=4.22.0->litellm->opik) (2024.10.1)\n",
      "Requirement already satisfied: referencing>=0.28.4 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from jsonschema<5.0.0,>=4.22.0->litellm->opik) (0.35.1)\n",
      "Requirement already satisfied: rpds-py>=0.7.1 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from jsonschema<5.0.0,>=4.22.0->litellm->opik) (0.22.3)\n",
      "Requirement already satisfied: mdurl~=0.1 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from markdown-it-py>=2.2.0->rich>=13.9.4->smolagents[telemetry,toolkit]) (0.1.2)\n",
      "Requirement already satisfied: python-dateutil>=2.8.2 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from pandas>=1.0->arize-phoenix->smolagents[telemetry,toolkit]) (2.9.0.post0)\n",
      "Requirement already satisfied: pytz>=2020.1 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from pandas>=1.0->arize-phoenix->smolagents[telemetry,toolkit]) (2024.2)\n",
      "Requirement already satisfied: tzdata>=2022.7 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from pandas>=1.0->arize-phoenix->smolagents[telemetry,toolkit]) (2024.1)\n",
      "Requirement already satisfied: greenlet!=0.4.17 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from sqlalchemy[asyncio]<3,>=2.0.4->arize-phoenix->smolagents[telemetry,toolkit]) (3.1.1)\n",
      "Requirement already satisfied: graphql-core<3.4.0,>=3.2.0 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from strawberry-graphql>=0.262.0->arize-phoenix->smolagents[telemetry,toolkit]) (3.2.5)\n",
      "Requirement already satisfied: regex>=2022.1.18 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from tiktoken>=0.7.0->litellm->opik) (2024.11.6)\n",
      "Requirement already satisfied: aiohappyeyeballs>=2.3.0 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from aiohttp->litellm->opik) (2.4.4)\n",
      "Requirement already satisfied: aiosignal>=1.1.2 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from aiohttp->litellm->opik) (1.3.2)\n",
      "Requirement already satisfied: frozenlist>=1.1.1 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from aiohttp->litellm->opik) (1.5.0)\n",
      "Requirement already satisfied: multidict<7.0,>=4.5 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from aiohttp->litellm->opik) (6.1.0)\n",
      "Requirement already satisfied: propcache>=0.2.0 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from aiohttp->litellm->opik) (0.2.1)\n",
      "Requirement already satisfied: yarl<2.0,>=1.17.0 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from aiohttp->litellm->opik) (1.18.3)\n",
      "Requirement already satisfied: cryptography in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from authlib->arize-phoenix->smolagents[telemetry,toolkit]) (43.0.3)\n",
      "Requirement already satisfied: types-awscrt in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from botocore-stubs->boto3-stubs>=1.34.110->boto3-stubs[bedrock-runtime]>=1.34.110->opik) (0.23.10)\n",
      "Requirement already satisfied: joblib>=1.2.0 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from scikit-learn->arize-phoenix->smolagents[telemetry,toolkit]) (1.4.2)\n",
      "Requirement already satisfied: threadpoolctl>=3.1.0 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from scikit-learn->arize-phoenix->smolagents[telemetry,toolkit]) (3.5.0)\n",
      "Requirement already satisfied: cffi>=1.12 in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from cryptography->authlib->arize-phoenix->smolagents[telemetry,toolkit]) (1.17.0)\n",
      "Requirement already satisfied: pycparser in /opt/homebrew/Caskroom/miniconda/base/envs/py312_llm_eval/lib/python3.12/site-packages (from cffi>=1.12->cryptography->authlib->arize-phoenix->smolagents[telemetry,toolkit]) (2.22)\n",
      "Downloading huggingface_hub-0.31.4-py3-none-any.whl (489 kB)\n",
      "Downloading openinference_instrumentation_smolagents-0.1.11-py3-none-any.whl (12 kB)\n",
      "Downloading smolagents-1.16.1-py3-none-any.whl (125 kB)\n",
      "Downloading openinference_instrumentation-0.1.30-py3-none-any.whl (25 kB)\n",
      "Downloading openinference_semantic_conventions-0.1.17-py3-none-any.whl (9.4 kB)\n",
      "Installing collected packages: openinference-semantic-conventions, huggingface-hub, smolagents, openinference-instrumentation, openinference-instrumentation-smolagents\n",
      "  Attempting uninstall: openinference-semantic-conventions\n",
      "    Found existing installation: openinference-semantic-conventions 0.1.12\n",
      "    Uninstalling openinference-semantic-conventions-0.1.12:\n",
      "      Successfully uninstalled openinference-semantic-conventions-0.1.12\n",
      "  Attempting uninstall: huggingface-hub\n",
      "    Found existing installation: huggingface-hub 0.27.0\n",
      "    Uninstalling huggingface-hub-0.27.0:\n",
      "      Successfully uninstalled huggingface-hub-0.27.0\n",
      "  Attempting uninstall: openinference-instrumentation\n",
      "    Found existing installation: openinference-instrumentation 0.1.18\n",
      "    Uninstalling openinference-instrumentation-0.1.18:\n",
      "      Successfully uninstalled openinference-instrumentation-0.1.18\n",
      "\u001b[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\n",
      "open-webui 0.5.4 requires langfuse==2.44.0, which is not installed.\n",
      "open-webui 0.5.4 requires authlib==1.3.2, but you have authlib 1.5.2 which is incompatible.\n",
      "open-webui 0.5.4 requires boto3==1.35.53, but you have boto3 1.36.21 which is incompatible.\n",
      "open-webui 0.5.4 requires chromadb==0.5.15, but you have chromadb 0.5.23 which is incompatible.\n",
      "open-webui 0.5.4 requires fastapi==0.111.0, but you have fastapi 0.115.12 which is incompatible.\n",
      "open-webui 0.5.4 requires google-generativeai==0.7.2, but you have google-generativeai 0.8.4 which is incompatible.\n",
      "open-webui 0.5.4 requires langchain==0.3.7, but you have langchain 0.3.24 which is incompatible.\n",
      "open-webui 0.5.4 requires langchain-community==0.3.7, but you have langchain-community 0.3.22 which is incompatible.\n",
      "open-webui 0.5.4 requires psycopg2-binary==2.9.9, but you have psycopg2-binary 2.9.10 which is incompatible.\n",
      "open-webui 0.5.4 requires pydantic==2.9.2, but you have pydantic 2.11.4 which is incompatible.\n",
      "open-webui 0.5.4 requires pypdf==4.3.1, but you have pypdf 5.4.0 which is incompatible.\n",
      "open-webui 0.5.4 requires python-multipart==0.0.18, but you have python-multipart 0.0.9 which is incompatible.\n",
      "open-webui 0.5.4 requires uvicorn[standard]==0.30.6, but you have uvicorn 0.34.2 which is incompatible.\u001b[0m\u001b[31m\n",
      "\u001b[0mSuccessfully installed huggingface-hub-0.31.4 openinference-instrumentation-0.1.30 openinference-instrumentation-smolagents-0.1.11 openinference-semantic-conventions-0.1.17 smolagents-1.16.1\n",
      "\n",
      "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m25.0.1\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m25.1.1\u001b[0m\n",
      "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "%pip install 'smolagents[telemetry,toolkit]' opik"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f47246a0",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "OPIK: Opik is already configured. You can check the settings by viewing the config file at /Users/jacquesverre/.opik.config\n"
     ]
    }
   ],
   "source": [
    "import opik\n",
    "\n",
    "opik.configure(use_local=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c9a0ad8a",
   "metadata": {},
   "source": [
    "## Preparing our environment\n",
    "\n",
    "The Opik integration supports all Smolagents clients, for this demo we will be using the OpenAI client and will therefore set our OpenAI API keys."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "106f4e32",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import getpass\n",
    "\n",
    "if \"OPENAI_API_KEY\" not in os.environ:\n",
    "    os.environ[\"OPENAI_API_KEY\"] = getpass.getpass(\"Enter your OpenAI API key: \")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "346352a2",
   "metadata": {},
   "source": [
    "We now need to configure the OpenTelemetry environment variables:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "ba1040e8",
   "metadata": {},
   "outputs": [],
   "source": [
    "import opik\n",
    "import os\n",
    "\n",
    "opik_config = opik.config.get_from_user_inputs()\n",
    "\n",
    "os.environ[\"OTEL_EXPORTER_OTLP_ENDPOINT\"] = (\n",
    "    \"https://www.comet.com/opik/api/v1/private/otel\"\n",
    ")\n",
    "os.environ[\"OTEL_EXPORTER_OTLP_HEADERS\"] = (\n",
    "    f\"Authorization={opik_config.api_key},projectName={opik_config.project_name},Comet-Workspace={opik_config.workspace}\"\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cdb9e20b",
   "metadata": {},
   "source": [
    "*Note 1:* The `projectName` and `Comet-Workspace` headers are optional and can be set to `Default` and `default` respectively.\n",
    "\n",
    "*Note 2:* If you are using the self-hosted version of Opik, you will need to update the environment variables defined above. You can learn more about this in the [Opik OpenTelemetry documentation](https://www.comet.com/docs/opik/opentelemetry/overview).\n",
    "\n",
    "## Setting up OpenTelemetry for SmolAgents\n",
    "\n",
    "To set up the Telemetry, you will need to add the following to your code:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "ce654006",
   "metadata": {},
   "outputs": [],
   "source": [
    "from opentelemetry.sdk.trace import TracerProvider\n",
    "\n",
    "from openinference.instrumentation.smolagents import SmolagentsInstrumentor\n",
    "from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter\n",
    "from opentelemetry.sdk.trace.export import SimpleSpanProcessor\n",
    "\n",
    "trace_provider = TracerProvider()\n",
    "trace_provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter()))\n",
    "\n",
    "SmolagentsInstrumentor().instrument(tracer_provider=trace_provider)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ea7f42e2",
   "metadata": {},
   "source": [
    "## Using the Smolagents library\n",
    "\n",
    "Now that everything is set up, we can use the Smolagents library and track the corresponding traces in Opik:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9c47b333",
   "metadata": {},
   "outputs": [],
   "source": [
    "from smolagents import CodeAgent, WebSearchTool, OpenAIServerModel\n",
    "\n",
    "model = OpenAIServerModel(\n",
    "    model_id=\"gpt-4o\",\n",
    ")\n",
    "\n",
    "agent = CodeAgent(tools=[WebSearchTool()], model=model, stream_outputs=True)\n",
    "\n",
    "agent.run(\n",
    "    \"How many seconds would it take for a leopard at full speed to run through Pont des Arts?\"\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ad38fafd",
   "metadata": {},
   "source": [
    "As we have configured the OpenTelemetry logging, the agent execution and all it's intermediate steps will be logged in Opik:\n",
    "\n",
    "![Smolagents Integration](https://raw.githubusercontent.com/comet-ml/opik/main/apps/opik-documentation/documentation/fern/img/cookbook/smolagents_trace_cookbook.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e81a2c92",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py312_llm_eval",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
